home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / arrays.exe / ARRAYS.CPP < prev    next >
C/C++ Source or Header  |  1991-10-29  |  2KB  |  71 lines

  1. /* ARRAYS.CPP 1.01 DEMONSTRATION OF C LANGUAGE ARRAY DIFFICULTIES
  2.  
  3.     V01 L01 1991-10-29-11:48 make C++ version to see if any difference.
  4.  
  5.         L00    1991-10-29-11:13 develop array case based on problem raised
  6.         by Barry Gombert.  How do you put a pointer to an array in
  7.         a struct definition?
  8.  
  9.         */
  10.  
  11. #include <assert.h>
  12. #include <stdlib.h>
  13.  
  14.  struct IBlock
  15.  
  16.     {unsigned long (*Band)[];};
  17.  
  18.  struct JBlock
  19.  
  20.     {unsigned long *BandArray;};
  21.  
  22.  
  23. int main(int argc, char *argv[])
  24.  
  25.    { /* figure out some Band array information */
  26.  
  27.  
  28.    unsigned long Band1[10];
  29.  
  30.    IBlock Block1;
  31.  
  32.    JBlock Block2;
  33.  
  34.    int i;
  35.  
  36.    i = sizeof(Band1)/sizeof(Band1[0]);
  37.    while (i--)
  38.      {Band1[i] = ((unsigned long)(1) << 16) + i; }
  39.  
  40.    Block1.Band = Band1;
  41.             // The rhs type really is incompatible!
  42.  
  43.    Block1.Band = (unsigned long (*)[]) Band1;
  44.             // Works a charm, as usual.
  45.    Block1.Band = new (unsigned long [10]);
  46.             // The same problem!
  47.  
  48.    Block1.Band = (unsigned long (*)[]) new (unsigned long [10]);
  49.             // The same solution ...
  50.  
  51.    assert(Block1.Band == Band1);
  52.  
  53.    assert(Block1.Band == (unsigned long (*)[]) Band1);
  54.  
  55.    assert(Block1.Band[0] == Band1[0]);
  56.             // An interesting variation on this problem.
  57.  
  58.    assert((*Block1.Band)[0] == Band1[0]);
  59.             // Feel better now?
  60.  
  61.    Block2.BandArray = Band1;
  62.             // The rhs type really is compatible with
  63.             // *this* lhs though!  So now we know what
  64.             // the value of (Band1) is, aye?
  65.  
  66.    assert(Block2.BandArray == Band1);
  67.  
  68.    assert(Block2.BandArray[0] == Band1[0]);
  69.  
  70.    return 0;}
  71.